<?php
/**
 * @file: clear_cache.php
 * @User: shiwuhao
 * @Date: 16/5/6 17:04
 * @synopsis: 清除缓存
 */

class Clear_cachem extends CI_Model
{
    public function __construct()
    {
        parent::__construct();

        ci_load_once('redis', 'cache');
    }

    /**
     * 清除帖子缓存
     * @param $post_id
     */
    public function clear_post($post_id)
    {
        if (!$post_id) return false;
        // redis
        $post_key = POST_INFO_STRING_PREFIX.$post_id;         // 帖子数据缓存 redis
        $img_key  = POST_IMG_INFO_PREFIX.$post_id;            // 帖子图片缓存 redis

        $vote_count_key  = VOTE_NUM_PREFIX . $post_id;        // 帖子投票总数
        $vote_option_key = VOTE_OPTION_NUM_PREFIX . $post_id; // 帖子投票选项总数

        $this->redis->delete($post_key);
        $this->redis->delete($img_key);
        $this->redis->delete($vote_count_key);
        $this->redis->delete($vote_option_key);

        // mc
        $mc_admin_post_key = 'one_post_for_server6_'.$post_id;
        $mc_post_key       = 'get_one_post_search_key_'.$post_id;
        $mc_img_key        = 'original_attachments_key1_'.$post_id;

        $this->mc->is_supported();
        $this->mc->delete($mc_admin_post_key);
        $this->mc->delete($mc_post_key);
        $this->mc->delete($mc_img_key);
        return true;
    }

    /**
     * 清除投票选项缓存
     * @param $post_id
     * @return bool
     */
    public function clear_vote($post_id)
    {
        if (!$post_id) return false;
        $vote_count_key  = VOTE_NUM_PREFIX . $post_id;        // 帖子投票总数
        $vote_option_key = VOTE_OPTION_NUM_PREFIX . $post_id; // 帖子投票选项总数
        $res = $this->redis->delete($vote_count_key);
        $res = $this->redis->delete($vote_option_key);
        return true;
    }

    /**
     * 清理帖子置顶缓存
     * @param $post_id
     */
    public function clear_top_post()
    {
        // redis
        $key = TOP_POST_PREFIX.'*';
        $keys = $this->redis->keys($key);

        if (is_array($keys) && $keys) {
            foreach ($keys as $v) {
                $this->redis->delete($v);
            }
        }

        // mc
        $ios_key     = 'mc_key_top_post_3001';
        $android_key = 'mc_key_top_post_2002';
        $this->mc->is_supported();
        $this->mc->delete($ios_key);
        $this->mc->delete($android_key);
        return true;
    }

    /**
     * 清理用户缓存
     * @param $user_id
     */
    public function clear_user($user_id)
    {
        if (!$user_id) return false;
        // mc
        $key1 = "getUserInfoById_1".$user_id;           //userinfo 缓存
        $key2 = "getUserInfoALLById_1".$user_id;        //userinfo缓存获取
        $key3 = "getUserInfoALLById_" . $user_id;       //用户信息缓存
        $key4 = 'userm_verified_info'. $user_id;        //userinfo缓存获取 用户认证信息的缓存
        $key5 = 'qy_expertm_get_exp_info_'.$user_id;

        $key6 = "getUserInfoById_2".$user_id;

        $this->mc->is_supported();
        $this->mc->delete($key1);
        $this->mc->delete($key2);
        $this->mc->delete($key3);
        $this->mc->delete($key4);
        $this->mc->delete($key5);
        $this->mc->delete($key6);
        return true;
    }

    /**
     * 清除所有专家列表缓存
     */
    public function clear_all_expert_list()
    {
        $key = 'expert_list_of_all';
        $this->mc->is_supported();
        $this->mc->delete($key);
        return true;
    }

    /**
     * 清除科室下专家列表缓存
     * @param $thread_id
     * @return bool
     */
    public function clear_thread_expert_list($thread_id)
    {
        if (!$thread_id) return false;
        $key = 'expert_list_of_thread_'.$thread_id;  //专家邀请列表  for 专家邀请帖
        $this->mc->is_supported();
        $this->mc->delete($key);
        return true;
    }

    /**
     * 清理专题列表,专题文章缓存
     * @param $disubject_id
     * @return bool
     */
    public function clear_disubject($disubject_id)
    {
        if (!$disubject_id) return false;
        $this->load->model('disubjectm');
        $key1 = "disubject_".$disubject_id;  //专题列表缓存
        $key2 = Disubjectm::GET_ONE_DISUBJECT.$disubject_id;  //专题文章内容缓存
        $this->mc->is_supported();
        $this->mc->delete($key1);
        $this->mc->delete($key2);
        return true;
    }

    /**
     * 清除用户订阅的科室,标签 缓存
     * @param $user_id
     */
    public function clear_user_rss($user_id)
    {
        if (!$user_id) return false;
        $key1 = MY_RSS_THREAD_PREFIX.$user_id;
        $key2 = MY_RSS_THREADTAT_PREFIX.$user_id;
        $key3 = MY_RSS_TOPIC_PREFIX.$user_id;
        $this->redis->delete($key1);
        $this->redis->delete($key2);
        $this->redis->delete($key3);

        $this->load->model('rssm');
        $mc_key1 = Rssm::MC_KEY_USER_RSS_KEY.$user_id;
        $mc_key2 = Rssm::MC_KEY_USER_RSS_KEY_2_1.$user_id;
        $this->mc->is_supported();
        $this->mc->delete($mc_key1);
        $this->mc->delete($mc_key2);

        $this->clear_user_unread($user_id);
        return true;
    }

    /**
     * 清空用户未读数量缓存
     * @param $user_id
     */
    public function clear_user_unread($user_id)
    {
        if (!$user_id) return false;
        $key = USER_INFO_PREFIX.$user_id;
        $hash_key1 = 'category_1';
        $hash_key2 = 'category_2_3';

        $this->redis->hSet($key, $hash_key1, 0);
        $this->redis->hSet($key, $hash_key2, 0);
        return true;
    }

    /**
     * 删除 用户未读消息数量缓存
     * @param $user_id
     * @param $apptype
     * @return bool
     */
    public function clear_msg_count($user_id, $apptype)
    {
        if (!$user_id || !$apptype) return false;
        $key = 'message_count_' . $user_id . "_" . $apptype;
        return $this->mc->delete($key);
    }

    /**
     * 删除用户专家咨询帖缓存的分页数据
     * @param int $user_id
     * @return bool
     */
    public function delete_cache_spe_page_data($user_id = 0)
    {
        if (!$user_id) return false;
        $key = MY_SPECIALIST_LIST_PREFIX . $user_id . ':*';
        $keys = $this->redis->keys($key);

        if (is_array($keys) && $keys) {
            foreach ($keys as $v) {
                $this->redis->delete($v);
            }
        }

        return true;
    }

    /**
     * 清除屏蔽列表缓存
     * @param $user_id
     * @return bool
     */
    public function clear_hate($user_id)
    {
        if (!$user_id) return false;
        $key = HATE_LIST_PREFIX.$user_id;
        $res = $this->redis->delete($key);
        return true;
    }


    /**
     * 删除用户回帖缓存的分页数据
     * @param int $user_id
     * @return bool
     */
    public function clear_my_reply_lists_cache($user_id = 0)
    {
        $key1 = MY_REPLY_LIST_PREFIX . $user_id . ':*';
        $key2 = MY_REPLY_LIST_NUM_ONE_PREFIX . $user_id . ':*';
        $key1s = $this->redis->keys($key1);
        $key2s = $this->redis->keys($key2);
        if (!$key1s && !$key2s) return true;
        if ($key1s && !$key2s) {
            $keys = $key1s;
        } elseif (!$key1s && $key2s) {
            $keys = $key2s;
        } else {
            $keys = array_merge($key1s, $key2s);
        }
        foreach ($keys as $v) {
            $this->redis->delete($v);
        }
        return true;
    }


    /**
     * 删除用户发帖缓存的分页数据
     * @param int $user_id
     * @return bool
     */
    public function clear_my_publish_list_cache($user_id = 0)
    {
        $key = MY_PUBLISH_LIST_PREFIX . $user_id . ':*';
        $keys = $this->redis->keys($key);
        if (!$keys) return true;
        foreach ($keys as $v) {
            $this->redis->delete($v);
        }
        return true;
    }

    /**
     * 清除用户 回帖缓存列表
     * @param int $user_id
     * @return bool
     */
    public function clear_user_reply_post($user_id = 0)
    {
        if (!$user_id) return false;

        $key1 = MY_REPLY_LIST_PREFIX.$user_id;
        $key2 = OTHER_REPLY_POST_LIST_PREFIX.$user_id;
        $this->redis->delete($key1);
        $this->redis->delete($key2);

        return true;
    }


    /**
     * 清除升级缓存
     * @param int $user_id
     * @return bool
     */
    public function clear_update_version($pro_id)
    {
        if (!$pro_id) return false;
        $key = "softupdateversion" . $pro_id;
        return $this->mc->delete($key);
    }



    public function clear_part_info($part_id)
    {
        if (!$part_id) return false;
        // mc
        $key = 'get_one_package_new_'.$part_id;           //part_info 缓存
        $this->mc->is_supported();
        $this->mc->save($key,[],0);
        return true;
    }

}